﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Spun.Naming.Paths
{
    public class PropertyPathNode : IPathNode
    {
        public PropertyPathNode(string PropertyName)
        {
            if (PropertyName == null)
                throw new ArgumentNullException();

            this.propertyName = PropertyName;
        }

        #region IPathNode Members

        public string PathElement
        {
            get { return '.' + PropertyName; }
        }

        public object Resolve(NamingContext Context, object RelativeTo)
        {
            return GetProp(RelativeTo).GetValue(RelativeTo, null);
        }

        public void Update(NamingContext Context, object RelativeTo, object Value)
        {
            GetProp(RelativeTo).SetValue(RelativeTo, Value, null);
        }

        public bool EquivalentToMemberName(string MemberName)
        {
            return PropertyName.Equals(MemberName);
        }
        #endregion

        public string PropertyName
        {
            get { return propertyName; }
        }

        #region Overrides
        public override bool Equals(object obj)
        {
            var ThatNode = obj as PropertyPathNode;

            if (ThatNode != null)
                return PropertyName.Equals(ThatNode.PropertyName, StringComparison.CurrentCultureIgnoreCase);
            else
                return false;
        }

        public override int GetHashCode()
        {
            return PropertyName.GetHashCode();
        }

        public override string ToString()
        {
            return '.' + PropertyName;
        }
        #endregion

        private PropertyInfo GetProp(object RelativeTo)
        {
            if (RelativeTo == null)
                throw new CouldNotResolvePath(
                    string.Format("Could not get the property named {0} off of a null object", PropertyName));

            var Property = RelativeTo.GetType().GetProperty(PropertyName);

            if (Property == null)
                throw new CouldNotResolvePath(string.Format("Could not get the property named {0} of an object of type {1}",
                    PropertyName, RelativeTo.GetType().FullName));

            return Property;
        }

        private string propertyName;
    }
}
